package sk.lukaskrupa.guess.the.color.ui;

import static sk.lukaskrupa.guess.the.color.core.ApplicationConstants.APP_SETTINS;
import static sk.lukaskrupa.guess.the.color.core.ApplicationConstants.HELP_FIRST_TIME_SHOW;
import sk.lukaskrupa.guess.the.color.listeners.MenuButtonListener;
import sk.lukaskrupa.guess.the.color.logging.Logging;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.Button;

/**
 * Menu activity, from which other activities are executed or exited.
 * 
 * @author Lukas Krupa (lukas0krupa@gmail.com)
 * 
 */
/**
 * @author Lukas Krupa (lukas0krupa@gmail.com)
 * 
 */
public class Menu extends Activity {

	private static final Logging logger = Logging.getLog(Menu.class);

	private static final int HELP_ACTIVITY_REQ_CODE = 0;
	private static final int GAME_REQUEST_CODE = 1;
	private static final int SETTINGS_REQUEST_CODE = 2;
	private static final int HELP_REQUEST_CODE = 3;

	private MenuButtonListener menuListener;

	private boolean firstTime;

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		logger.info("onCreate", "Creating Menu activity.");
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onStart() {
		super.onStart();

		logger.info("onStart", "Starting Menu activity.");
		setContentView(R.layout.menu);

		this.menuListener = new MenuButtonListener(this);

		((Button) findViewById(R.id.newGame)).setOnClickListener(this.menuListener);
		((Button) findViewById(R.id.settings)).setOnClickListener(this.menuListener);
		((Button) findViewById(R.id.help)).setOnClickListener(this.menuListener);
		((Button) findViewById(R.id.exit)).setOnClickListener(this.menuListener);

		this.firstTime = getSharedPreferences(APP_SETTINS, MODE_PRIVATE).getBoolean(HELP_FIRST_TIME_SHOW, true);
		if (this.firstTime) {
			Intent helpDialog = new Intent(getApplicationContext(), Help.class);

			this.startActivityForResult(helpDialog, HELP_ACTIVITY_REQ_CODE);
		}

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == HELP_ACTIVITY_REQ_CODE) {
			this.firstTime = false;
			logger.debug("onPause", "Saving first time help dialog show indicator.");

			Editor saveEditor = getSharedPreferences(APP_SETTINS, Context.MODE_PRIVATE).edit();
			saveEditor.putBoolean(HELP_FIRST_TIME_SHOW, this.firstTime);
			saveEditor.apply();
			saveEditor.commit();

			logger.debug("onPause", "First time help dialog show indicator saved.");
		}
	}
	/**
	 * Starts Game activity.
	 * 
	 * @param appSettings2
	 */
	public void startGame() {
		Intent game = new Intent(getApplicationContext(), Game.class);
		startActivityForResult(game, GAME_REQUEST_CODE);
	}

	/**
	 * Starts Game activity.
	 */
	public void startSettings() {
		logger.debug("startSettings", "Starting Settings activity");
		Intent settings = new Intent(getApplicationContext(), Settings.class);
		startActivityForResult(settings, SETTINGS_REQUEST_CODE);
	}

	/**
	 * Starts Game activity.
	 */
	public void startHelp() {
		logger.debug("startHelp", "Starting Help activity");
		Intent help = new Intent(getApplicationContext(), Help.class);
		startActivityForResult(help, HELP_REQUEST_CODE);
	}

}
